﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RLRT.Common;
using RLRT.Models;
using Microsoft.Reporting.WebForms;

namespace RLRT.Web
{
    public partial class PrintReport : LocalDialog
    {
        #region 属性
        private string CurrentID
        {
            get
            {
                object tempObject = ViewState["CurrentID"];
                return (tempObject != null) ? (string)tempObject : string.Empty;
            }
            set
            {
                ViewState["CurrentID"] = value;
            }
        }

        private string CurrentSource
        {
            get
            {
                object tempObject = ViewState["CurrentSource"];
                return (tempObject != null) ? (string)tempObject : string.Empty;
            }
            set
            {
                ViewState["CurrentSource"] = value;
            }
        }

        private string RTType
        {
            get
            {
                object tempObject = ViewState["RTType"];
                return (tempObject != null) ? (string)tempObject : string.Empty;
            }
            set
            {
                ViewState["RTType"] = value;
            }
        }
        private string RTNoFrom
        {
            get
            {
                object tempObject = ViewState["RTNoFrom"];
                return (tempObject != null) ? (string)tempObject : string.Empty;
            }
            set
            {
                ViewState["RTNoFrom"] = value;
            }
        }
        private string RTNoTo
        {
            get
            {
                object tempObject = ViewState["RTNoTo"];
                return (tempObject != null) ? (string)tempObject : string.Empty;
            }
            set
            {
                ViewState["RTNoTo"] = value;
            }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                #region 获取参数
                string id = Request.QueryString["ID"];
                if (id != null && id.Trim().Length != 0)
                {
                    this.CurrentID = id.Trim();
                }

                string source = Request.QueryString["Source"];
                if (source != null && source.Trim().Length != 0)
                {
                    this.CurrentSource = source.Trim();
                }

                string rtType = Request.QueryString["rtType"];
                if (rtType != null && rtType.Trim().Length != 0)
                {
                    this.RTType = rtType.Trim();
                }
                string rtNoFrom = Request.QueryString["rtNoFrom"];
                if (rtNoFrom != null && rtNoFrom.Trim().Length != 0)
                {
                    this.RTNoFrom = rtNoFrom.Trim();
                }
                string rtNoTo = Request.QueryString["rtNoTo"];
                if (rtNoTo != null && rtNoTo.Trim().Length != 0)
                {
                    this.RTNoTo = rtNoTo.Trim();
                }

                #endregion

                //初始化
                this.Initialize();
            }
        }

        /// <summary>
        /// 初始化
        /// </summary>
        private void Initialize()
        {
            string rtNo = this.CurrentID;

            DataTable table = new DataTable();
            using (RTProvider rtProvider = new RTProvider())
            {
                if (CurrentSource == "maintain")
                {
                    var rt = rtProvider.GetRTType(rtNo);
                    if (rt == null)
                    {
                        Response.Write("收货单号码有误！");
                        Response.End();
                        return;
                    }

                    string rtType = rt[0].ToString().Trim();
                    if (string.IsNullOrWhiteSpace(rtType))
                    {
                        Response.Write("收货单号码有误！");
                        Response.End();
                        return;
                    }

                    this.RTType = rtType;
                    //请购数据源
                    table = rtProvider.GetRTMainPrint(this.RTType, rtNo);

                }
                else if (CurrentSource == "print")
                {
                    table = rtProvider.GetRTOrderPrint(this.RTType, this.RTNoFrom, this.RTNoTo);
                }
            }

            string report = "RLRT.Web.RT" + this.RTType + ".rdlc";
            Viewer.LocalReport.ReportEmbeddedResource = report;
            Viewer.LocalReport.DataSources.Clear();
            Viewer.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", table));
        }
    }
}